package com.baizhi.utils.guava;

import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import org.junit.Test;

import java.util.Collection;
import java.util.Map;

public class MultimapTest {

    /**
     * Multimap，用来替代JDK原生的Map<String, Collection<String>>
     */
    @Test
    public void test1() {
        Multimap<String, String> multimap = HashMultimap.create();
        multimap.put("a", "1");
        multimap.put("a", "2");
        multimap.put("a", "3");
        Collection<String> aValues = multimap.get("a");
        System.out.println(aValues);

        // 是否包含key=a,value=1的entry
        System.out.println(multimap.containsEntry("a", "1"));
        System.out.println(multimap.containsEntry("a", "4"));

        // 转化成JDK原生api实现的数据结构
        Map<String, Collection<String>> jdkMap = multimap.asMap();
        System.out.println(jdkMap);
    }
}
